package com.wgts.wgts.mvp.view.activity.shopkeeper;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;
import com.wgts.wgts.R;
import com.wgts.wgts.base.LazyBaseFragment;
import com.wgts.wgts.control.dialog.WonderLoadingDialog;
import com.wgts.wgts.dagger.ComponentHolder;
import com.wgts.wgts.dagger.component.DaggerAssetDetailFragmentComponent;
import com.wgts.wgts.dagger.module.AssetDetailFragmentModule;
import com.wgts.wgts.mvp.presenter.IAssetDetailFragmentPresenter;
import com.wgts.wgts.mvp.view.IAssetDetailFragmentView;
import com.wgts.wgts.netmodle.resp.WgtsKeepAssetEntity;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class AssetDetailFragment extends LazyBaseFragment implements IAssetDetailFragmentView {

    private static final String TYPE = "type";
    @BindView(R.id.recycle_view)
    RecyclerView recycleView;
    Unbinder unbinder;

    AssetDetailAdapter adapter;

    @Inject
    IAssetDetailFragmentPresenter presenter;
    @BindView(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;

    @BindView(R.id.none_data_ly)
    LinearLayout noneDataLy;
    @BindView(R.id.net_error_ly)
    LinearLayout netErrorLy;

    private WonderLoadingDialog dialog;

    private List<WgtsKeepAssetEntity> list;


    @Override
    protected void loadData() {
        switch (getArguments().getInt(TYPE)) {
            case 0:
                showDialog();
                presenter.initAssets("");
                break;
            case 1:
                showDialog();
                presenter.initAssets("TASK_PROFIT");
                break;
            case 2:
                showDialog();
                presenter.initAssets("SPREAD_AWARD");
                break;
            case 3:
                showDialog();
                presenter.initAssets("TASK_REBATE");
                break;
            case 4:
                showDialog();
                presenter.initAssets("WITHDRAW");
                break;
        }

    }

    @Override
    public void init() {
        DaggerAssetDetailFragmentComponent
                .builder()
                .assetDetailFragmentModule(new AssetDetailFragmentModule(this))
                .appComponent(ComponentHolder.getInstance().getAppComponent())
                .build()
                .inject(this);
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.asset_detail_fragment, container, false);
        unbinder = ButterKnife.bind(this, view);
        initViw();
        return view;
    }


    public static AssetDetailFragment getInstance(int type) {
        AssetDetailFragment fragment = new AssetDetailFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(TYPE, type);
        fragment.setArguments(bundle);
        return fragment;
    }

    private void initViw() {
        dialog = new WonderLoadingDialog(getActivity());
        refreshLayout.setEnableLoadmore(true);
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                presenter.loadMore();
            }
        });

        refreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                presenter.loadMore();
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {

                switch (getArguments().getInt(TYPE)) {
                    case 0:

                        presenter.initAssets("");
                        break;
                    case 1:

                        presenter.initAssets("TASK_PROFIT");
                        break;
                    case 2:

                        presenter.initAssets("SPREAD_AWARD");
                        break;
                    case 3:

                        presenter.initAssets("TASK_REBATE");
                        break;
                    case 4:

                        presenter.initAssets("WITHDRAW");
                        break;
                }
            }
        });

        list = new ArrayList<>();
        adapter = new AssetDetailAdapter(getContext(), list);
        recycleView.setLayoutManager(new LinearLayoutManager(getContext()));
        recycleView.setAdapter(adapter);


    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @Override
    public void onError(int status, String msg) {
        dismissDialog();
        netErrorLy.setVisibility(View.VISIBLE);
    }

    @Override
    public void assetResult(List<WgtsKeepAssetEntity> entity, boolean noMore) {
        dismissDialog();
        netErrorLy.setVisibility(View.GONE);
        refreshLayout.finishRefresh();
        if (entity.size() == 0) {
            noneDataLy.setVisibility(View.VISIBLE);
        } else {
            noneDataLy.setVisibility(View.GONE);
        }

        adapter.setNoMore(noMore);

        if (noMore) {
            refreshLayout.setEnableLoadmore(false);
        } else {
            refreshLayout.setEnableLoadmore(true);
        }


        list.clear();
        list.addAll(entity);
        adapter.notifyDataSetChanged();

    }

    @Override
    public void moreAssetResult(List<WgtsKeepAssetEntity> entity, boolean hasMore) {
        netErrorLy.setVisibility(View.GONE);
        refreshLayout.finishLoadmore();
        adapter.setNoMore(hasMore);

        if (hasMore) {
            refreshLayout.setEnableLoadmore(false);
        } else {
            refreshLayout.setEnableLoadmore(true);
        }

        list.addAll(entity);
        adapter.notifyDataSetChanged();
    }

    private void showDialog() {
        if (dialog != null) {
            dialog.show();
        }
    }

    private void dismissDialog() {
        if (dialog != null) {
            if (dialog.isShowing()) {
                dialog.dismiss();
            }
        }
    }
}
